﻿using Microsoft.EntityFrameworkCore.Metadata.Internal;
using QInfrastructure.Interfaces;
using QInfrastructure.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace QInfrastructure.Services
{
    public class SysQCacheService : QBaseMainService, TSysQCache
    {
        public void AddOrUpdateQCache<T>(string key, List<T> info)
        {
           QInfrastructureCache.AddOrUpdateQCache(key, info);
        }

        public void AddQCacheAction(string key, Action action)
        {
            QInfrastructureCache.CacheActions.Add(key, action);
        }

        public List<T> GetEntityFromDb<T>(Expression<Func<T, bool>> express = null) where T : class, new()
        {
            using var db = CreateContext();
            var query = db.Set<T>().AsQueryable();
            if (express != null) query = query.Where(express);
            var res = query.ToList();
            return res;
        }

        public List<T> GetQCache<T>(string key)
        {
            return QInfrastructureCache.GetQCache<T>(key);
        }

        public void RemoveQCache(string key)
        {
            QInfrastructureCache.RemoveQCache(key);
        }

        public void RemoveQCache<T>()
        {
            var key = typeof(T).FullName;
            RemoveQCache(key);
        }
        public void AddOrUpdateQCacheAction<T>(List<T> lst)
        {
            var key = typeof(T).FullName;
            AddOrUpdateQCache(key, lst);
        }
        public List<T> GetQCache<T>()
        {
            var key = typeof(T).FullName;
            return QInfrastructureCache.GetQCache<T>(key);
        }






    }
}
